const Router = require('koa-router');
const fs = require('fs')
const path = require('path')
const router = new Router({ prefix: '/api/v1/file' });
const config = require('../../config/index.js');

router.post('/upload',(ctx)=>{
    // 上传单个文件
  const file = ctx.request.files.file; // 获取上传文件
  // 创建可读流
  const reader = fs.createReadStream(file.path);
  let filePath = path.join(__dirname, '../../public/upload') + `/${file.name}`;
  // 创建可写流
  const upStream = fs.createWriteStream(filePath);
  // 可读流通过管道写入可写流
  reader.pipe(upStream);
  return ctx.body = {
        url: `${config.uploadUrl}/${file.name}`,
        code: 0,
        message: '上传成功'
  };
})

router.post('/uploadfiles', async (ctx, next) => {
    // 上传多个文件
    const files = ctx.request.files.file; // 获取上传文件
    console.log(files instanceof Array)
    if(files instanceof Array){
      for (let file of files) {
        // 创建可读流
        const reader = fs.createReadStream(file.path);
        // 获取上传文件扩展名
        let filePath = path.join(__dirname, '../../public/upload') + `/${file.name}`;
        // 创建可写流
        const upStream = fs.createWriteStream(filePath);
        // 可读流通过管道写入可写流
        reader.pipe(upStream);
      }
    }else{
        // 上传单个文件
        const file = ctx.request.files.file; // 获取上传文件
        // 创建可读流
        const reader = fs.createReadStream(file.path);
        let filePath = path.join(__dirname, '../../public/upload') + `/${file.name}`;
        // 创建可写流
        const upStream = fs.createWriteStream(filePath);
        // 可读流通过管道写入可写流
        reader.pipe(upStream);
        return ctx.body = {
              url: `${config.uploadUrl}/${file.name}`,
              code: 0,
              message: '上传成功'
        };
    }
    
    return ctx.body =  {
        success: true,
        data: {
          text: '上传成功',
          time: new Date().getTime(),
        }
    };
  });


module.exports = router